package areamanger

import (
	"backend/models"
	"backend/pkg/app"
	"backend/pkg/e"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
)

func GetVolume(c *gin.Context) { //查看仓库剩余容量

	err, resu, total := models.GetDeport() //获取所有仓库的容量等信息
	if err != nil {
		fmt.Println(err.Error()) //展示错误
		//app.Error(c, 2001, err, err.Error())
		return
	}
	app.Ok(c, map[string]interface{}{"value": resu, "total": total}, "ok") //成功后返回信息
}

func Getmessage(c *gin.Context) { //获取某个仓库详细信息
	page := -1
	if arg := c.Query("page"); arg != "" {
		page = com.StrTo(arg).MustInt()
	}
	limit := -1
	if arg := c.Query("limit"); arg != "" { //获取请求查询的信息
		limit = com.StrTo(arg).MustInt()
	}
	searchText := ""
	if arg := c.Query("searchText"); arg != "" {
		searchText = arg
	}
	storageParam := map[string]interface{}{ //将请求信息整合
		"page":       page,
		"limit":      limit,
		"searchText": searchText,
	}
	err, info, total := models.GetDetail(storageParam) //调用对应的取数函数
	if err != nil {
		fmt.Println(err.Error()) //后端显示错误
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.Ok(c, map[string]interface{}{"value": info, "total": total}, "OK") //将数据发送到对应的端口
}

func AddGoods(c *gin.Context) { //添加货物入库
	err, info := models.Add(c)
	if err != nil {
		fmt.Println(err.Error())                //后端显示错误
		app.Error(c, e.ERROR, err, err.Error()) //返回错误
		return
	}
	app.Ok(c, info, "ok")
}

func TakeGoods(c *gin.Context) { //取货操作
	err, info := models.Take(c)
	if err != nil {
		fmt.Println(err.Error())                //后端显示错误
		app.Error(c, e.ERROR, err, err.Error()) //返回错误
		return
	}
	app.Ok(c, map[string]interface{}{"value": info}, "ok")
}

func Getoutload(c *gin.Context) { //获取出库明细
	searchText := ""
	if arg := c.Query("searchText"); arg != "" {
		searchText = arg
	}
	storageParam := map[string]interface{}{ //将请求信息整合
		"searchText": searchText,
	}
	err, info := models.Getoutload(storageParam)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
	}
	app.Ok(c, map[string]interface{}{"value": info}, "ok")
}
